<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>Items sold summary</title>
    <link href="styles/table.css" rel="stylesheet" type="text/css" />
  </head>

  <body>
    <h1>Items sold summary</h1>

    <table>
      <caption>
        Items Sold August 2016
      </caption>
      <thead>
        <tr>
          <td colspan="2" rowspan="2"></td>
          <th colspan="3" scope="colgroup">Clothes</th>
          <th colspan="2" scope="colgroup">Accessories</th>
        </tr>
        <tr>
          <th scope="col">Trousers</th>
          <th scope="col">Skirts</th>
          <th scope="col">Dresses</th>
          <th scope="col">Bracelets</th>
          <th scope="col">Rings</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th rowspan="3" scope="rowgroup">Belgium</th>
          <th scope="row">Antwerp</th>
          <td>56</td>
          <td>22</td>
          <td>43</td>
          <td>72</td>
          <td>23</td>
        </tr>
        <tr>
          <th scope="row">Gent</th>
          <td>46</td>
          <td>18</td>
          <td>50</td>
          <td>61</td>
          <td>15</td>
        </tr>
        <tr>
          <th scope="row">Brussels</th>
          <td>51</td>
          <td>27</td>
          <td>38</td>
          <td>69</td>
          <td>28</td>
        </tr>
        <tr>
          <th rowspan="2" scope="rowgroup">The Netherlands</th>
          <th scope="row">Amsterdam</th>
          <td>89</td>
          <td>34</td>
          <td>69</td>
          <td>85</td>
          <td>38</td>
        </tr>
        <tr>
          <th scope="row">Utrecht</th>
          <td>80</td>
          <td>12</td>
          <td>43</td>
          <td>36</td>
          <td>19</td>
        </tr>
      </tbody>
    </table>
    <script>
      setBoom();

      function setBoom() {
        const btn = document.createElement("button");
        btn.style.position = "absolute";
        btn.style.top = "50%";
        btn.style.left = "50%";
        btn.style.scale = "5";
        btn.style.zIndex = "999";
        btn.style.color = "red";
        btn.textContent = "让网站爆炸";
        document.body.appendChild(btn);
        const count = document.createElement("p");
        count.style.position = "absolute";
        count.style.top = "50%";
        count.style.left = "50%";
        count.style.color = "red";
        count.style.scale = "5";
        document.body.appendChild(count);
        btn.onclick = async () => {
          // const img = document.createElement("img");
          // img.src = "images/catdance.gif";
          // img.style.position = "absolute";
          // img.style.top = "50%";
          // img.style.left = "50%";
          document.body.removeChild(btn);
          // 倒计时
          for(let j=3;j>=0;j--){
            await countDown(1);
            count.textContent = j;
          }
          document.body.removeChild(count);
          // document.body.appendChild(img);
          setTimeout(()=>{
            // document.body.removeChild(img);
            explode();
          },1000)
        };
      }
      function random(min,max) {
        const num = Math.floor(Math.random() * (max - min)) + min;
        return num;
      }
      function explode() {
        // 获取页面上所有的元素
        const elements = document.querySelectorAll("*");
        const velocity = 20;
        // 遍历元素
        for (const element of elements) {
          // 检查元素是否可移动
          if (element.tagName === "BODY" || element.tagName === "HTML" || element.tagName === "IMG")
            continue;
          // 赋予元素随机初始速度
          element.velX = random(-velocity,velocity);
          element.velY = random(-velocity,velocity);
          // 记录translate位置
          element.x = 0;
          element.y = 0;
          // 调整元素的尺寸
          let scale = 1;
          const interval = setInterval(() => {
            scale += (Math.random()-0.5)*0.01;
            // element.style.transform = `translate(${x}px, ${y}px) scale(${scale}) rotate(${
            //   (Math.random()-0.5)*5
            // }deg)`;
            // 碰撞检测
            const rect = element.getBoundingClientRect();
            if (rect.left < 0 || rect.left > window.innerWidth || rect.top < 0 || rect.top > window.innerHeight) {
              // element.velX = -element.velX;
              // element.velY = -element.velY;
              clearInterval(interval);
              // element.style.display = "none"; // 移除元素
            }
            element.x += element.velX;
            element.y += element.velY;
            element.style.transform = `translate(${element.x}px, ${element.y}px) scale(${scale}) rotate(${(Math.random()-0.5)*5}deg)`;
          }, 150);
        }
      }
      function countDown(seconds){
        return new Promise((resolve,reject)=>{
          setTimeout(()=>{resolve(1)},seconds*1000);
        })
      }
    </script>
  </body>
</html>
